//
//  ClassCell.m
//  GIFShow
//
//  Created by lanou11air029 on 15/4/9.
//  Copyright (c) 2015年 CK. All rights reserved.
//

#import "ClassCell.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface ClassCell ()
@property (strong, nonatomic) UIImageView * backImageView;
@property (strong, nonatomic) UILabel * mainTitleLabel;
@property (strong, nonatomic) UIView * backRoundRectView;
@property (strong, nonatomic) CAGradientLayer * gradientLayer;
@end
@implementation ClassCell

- (void)awakeFromNib {
    // Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupSubviews];
    }
    return self;
}
-(void)setupSubviews{
    self.backRoundRectView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 10)];
    self.mainTitleLabel = [[UILabel alloc]init];
    self.mainTitleLabel.font = [UIFont systemFontOfSize:14];
    self.backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(8, 0, 10, 10)];
    [self.contentView addSubview:self.backRoundRectView];
    [self.backRoundRectView addSubview:self.mainTitleLabel];
    [self.backRoundRectView addSubview:self.backImageView];
    
    self.backRoundRectView.layer.borderWidth = 1;
    self.backRoundRectView.layer.cornerRadius = 10;
    self.backRoundRectView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.backRoundRectView.layer.masksToBounds = YES;
    
    self.gradientLayer = [[CAGradientLayer alloc]init];

    self.gradientLayer.colors = @[(id)[UIColor colorWithRed:1 green:0.5 blue:0.5 alpha:1].CGColor,(id)[UIColor colorWithWhite:1 alpha:0].CGColor,(id)[UIColor colorWithWhite:1 alpha:0].CGColor,(id)[UIColor colorWithWhite:1 alpha:0].CGColor,(id)[UIColor colorWithRed:1 green:0.5 blue:0.5 alpha:0.2].CGColor];
    self.gradientLayer.locations = @[@0,@0.1,@0.5,@0.9,@1];
    self.gradientLayer.startPoint = CGPointMake(0, 0);
    self.gradientLayer.endPoint = CGPointMake(1, 1);
    [self.backRoundRectView.layer addSublayer:self.gradientLayer];
//    self.contentView.backgroundColor = [UIColor colorWithRed:0.9 green:1 blue:0.9 alpha:0.9];
    

}
-(void)setClassModel:(ClassLabelModel *)classModel{
    if (_classModel != classModel) {
        self.mainTitleLabel.text = classModel.label_content;
        [self.backImageView sd_setImageWithURL:[NSURL URLWithString:classModel.image_src] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        self.backImageView.layer.cornerRadius = 10;
        self.backImageView.layer.masksToBounds = YES;
        CGRect imageViewFrame = self.backImageView.frame;
        imageViewFrame.origin.x = 8;
        imageViewFrame.origin.y = 2;
        imageViewFrame.size = CGSizeMake(self.frame.size.width-20, (self.frame.size.width-20)/classModel.image_w * classModel.image_h);
        self.backImageView.frame = imageViewFrame;
        self.mainTitleLabel.frame = CGRectMake(0, imageViewFrame.origin.y+imageViewFrame.size.height, 1, 1);
        [self.mainTitleLabel sizeToFit];
        CGPoint center = self.mainTitleLabel.center;
        center.x = self.center.x;
        self.mainTitleLabel.center = center;
        self.backRoundRectView.frame = CGRectMake(2, 2, self.frame.size.width - 4, self.mainTitleLabel.frame.size.height+self.backImageView.frame.size.height + 4);
        self.gradientLayer.frame = self.backRoundRectView.bounds;
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if (selected) {
        self.contentView.backgroundColor = [UIColor colorWithRed:0.8 green:1 blue:0.9 alpha:1];
    }else{
        self.contentView.backgroundColor = [UIColor whiteColor];
    }
    // Configure the view for the selected state
}

@end
